package com.aimuceliang.aimuceliang.utils;

import android.os.Handler;
import android.os.Message;

import java.util.ArrayList;
import java.util.Collections;

/**
 * Created by sa on 2017/9/11.
 */
public class DataUtils {
    private Handler mHandler;
    public static final int NUM = 10;
    public static final int TOTAL_WEIGHT = 11;
    public static final int MAX_WEIGHT = 12;
    public static final int MIN_WEIGHT = 13;
    public static final int AVERAGE = 14;

    public DataUtils(Handler mHandler) {
        this.mHandler = mHandler;
    }

    private static ArrayList<Float> analyseBytes(ArrayList<Byte> values) {
        //02 2D 30 21 20 20 31 34 37 36 20 20 30 30 30 30 20 20 30 37 33 38 20 20 32 0D
        // 0  1  2  3  4  5  6  7  8  9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
        //开始
        ArrayList<Float> datas = new ArrayList<>();
        String hStr = Integer.toBinaryString(TimeUtil.getIntFromByte(values.get(1)));

        float len1 = TimeUtil.getScale(TimeUtil.get2StyleFromHexStr1(hStr));//前面小数位置

        byte[] buf = new byte[6];
        datas.add((float) (TimeUtil.getIntFromByte(values.get(3)) & 0x01));  //这里要取最后一个bit,
        //总重  皮重，当前重量       1
        for (int j = 0; j < 3; j++) {
            for (int i = 0; i < 6; i++) {
                if (values.get(i + 4 + j * 6) == 0x20) //如果是空格，赋值为0
                {
                    buf[i] = 0;
                } else {
                    buf[i] = (byte) (values.get(i + 4 + j * 6) - 0x30);  //ASCii字符转数字
                }
            }
            datas.add((TimeUtil.getIntFromByte(buf[0]) * 100000 + TimeUtil.getIntFromByte(buf[1]) * 10000 +
                    TimeUtil.getIntFromByte(buf[2]) * 1000 + TimeUtil.getIntFromByte(buf[3]) * 100
                    + TimeUtil.getIntFromByte(buf[4]) * 10 + TimeUtil.getIntFromByte(buf[5])) / len1);
        }

        //当前第几头猪  4
        for (int i = 0; i < 3; i++) {
            if (values.get(i + 22) == 0x20) //如果是空格，赋值为0
            {
                buf[i] = 0;
            } else {
                buf[i] = (byte) (values.get(i + 22) - 0x30);
            }
        }
        datas.add((float) (TimeUtil.getIntFromByte(buf[0]) * 100 + TimeUtil.getIntFromByte(buf[1]) * 10 + TimeUtil.getIntFromByte(buf[2])));
        return datas;
    }


    public void ansyBlueData(byte[] data) {

        ArrayList<Byte> locaBytes = new ArrayList<>();

        for (int i = 0; i < data.length; i++) {
            if (data[i] == 0x02) {
                locaBytes.clear();
            }
            locaBytes.add(data[i]);
        }
        if (locaBytes.size() >= 26 && locaBytes.get(locaBytes.size() - 1) == 0x0D) {
            setBlueData(new ArrayList<>(locaBytes));
            locaBytes.clear();
        }
    }

    private static ArrayList<Float> allWeights = new ArrayList<>();

    private void setBlueData(ArrayList<Byte> datas) {
        try {
            ArrayList<Float> ps = analyseBytes(datas);

            if ((ps.get(0) == 1)) {  //每次一头猪会有很多个相同数据过来，0变1记录一次即可
                allWeights.add(ps.get(3));
                sendNum(allWeights.size());
                sendTotal(ps.get(1));
                sendMax(Collections.max(allWeights));
                sendMin(Collections.min(allWeights));
                sendAverage(TimeUtil.getAverage(allWeights));
                System.out.println("称重多少头::" + allWeights.size());//称重多少头
                System.out.println("总重::" + ps.get(1) + "");//总重
                System.out.println("最重::" + Collections.max(allWeights) + "");//最重
                System.out.println("平均重量::" + TimeUtil.getAverage(allWeights) + "");//平均重量
                System.out.println("最轻::" + Collections.min(allWeights) + "");//最轻
            }
        } catch (Exception e) {
            return;
        }
    }

    public void sendNum(int size) {
        if (allWeights == null || mHandler == null) {
            return;
        }
        Message msg = Message.obtain();
        msg.what = NUM;
        msg.obj = size;
        mHandler.sendMessage(msg);
    }

    public void sendTotal(float total) {
        if (allWeights == null || mHandler == null) {
            return;
        }
        Message msg = Message.obtain();
        msg.what = TOTAL_WEIGHT;
        msg.obj = total;
        mHandler.sendMessage(msg);
    }

    public void sendAverage(float ave) {
        if (allWeights == null || mHandler == null) {
            return;
        }
        Message msg = Message.obtain();
        msg.what = AVERAGE;
        msg.obj = ave;
        mHandler.sendMessage(msg);
    }

    public void sendMax(float max) {
        if (allWeights == null || mHandler == null) {
            return;
        }
        Message msg = Message.obtain();
        msg.what = MAX_WEIGHT;
        msg.obj = max;
        mHandler.sendMessage(msg);
    }

    public void sendMin(float min) {
        if (allWeights == null || mHandler == null) {
            return;
        }
        Message msg = Message.obtain();
        msg.what = MIN_WEIGHT;
        msg.obj = min;
        mHandler.sendMessage(msg);
    }
//    public static void main(String[] args) {
//        byte[] data = {0x02, 0x2D, 0x30, 0x21, 0x20, 0x20, 0x31, 0x34, 0x37, 0x36, 0x20, 0x20, 0x30, 0x30, 0x30, 0x30, 0x20, 0x20, 0x30, 0x37, 0x33, 0x38, 0x20, 0x20, 0x32, 0x0D};
//        ansyBlueData(data);
//    }
}
